package response;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.*;

/**
 * 演示使用 response 对象发送字节流
 */
public class SendStreamBody extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        InputStream in = getServletContext().getResourceAsStream("/res/images/header.jpg");
        OutputStream out = resp.getOutputStream();

        byte[] buffer = new byte[1024];
        int len = in.read(buffer);
        while (len != -1) {
            out.write(buffer, 0, len);
            len = in.read(buffer);
        }

    }
}
